package cn.gemiman.daoyi.module.cms.dal.mysql.articledetail;

import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.mybatis.core.mapper.BaseMapperX;
import cn.gemiman.daoyi.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.gemiman.daoyi.module.cms.controller.admin.articledetail.vo.ArticleDetailPageReqVO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.articledetail.ArticleDetailDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 文章 Mapper
 *
 * @author 道一源码
 */
@Mapper
public interface ArticleDetailMapper extends BaseMapperX<ArticleDetailDO> {

    default PageResult<ArticleDetailDO> selectPage(ArticleDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ArticleDetailDO>()
                .eqIfPresent(ArticleDetailDO::getAppSiteId, reqVO.getAppSiteId())
                .eqIfPresent(ArticleDetailDO::getAppId, reqVO.getAppId())
                .eqIfPresent(ArticleDetailDO::getCategoryId, reqVO.getCategoryId())
                .likeIfPresent(ArticleDetailDO::getTitle, reqVO.getTitle())
                .likeIfPresent(ArticleDetailDO::getSubTitle, reqVO.getSubTitle())
                .eqIfPresent(ArticleDetailDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ArticleDetailDO::getViewNum, reqVO.getViewNum())
                .eqIfPresent(ArticleDetailDO::getSort, reqVO.getSort())
                .betweenIfPresent(ArticleDetailDO::getCreateTime, reqVO.getCreateTime())
                .likeIfPresent(ArticleDetailDO::getTags, reqVO.getTags())
                .eqIfPresent(ArticleDetailDO::getOriginType, reqVO.getOriginType())
                .orderByAsc("createTime".equals(reqVO.getOrderBy()), ArticleDetailDO::getCreateTime)
                .orderByDesc("createTimeDesc".equals(reqVO.getOrderBy()), ArticleDetailDO::getCreateTime)
                .orderByAsc(ArticleDetailDO::getSort)
                .orderByDesc(ArticleDetailDO::getViewNum)
                .orderByDesc(ArticleDetailDO::getId));
    }

    @Update("update cms_article_detail set view_num = view_num + 1 where id = #{id}")
    void updateReadCount(@Param("id") Long id);

    @Update("update cms_article_detail set thumbs_up_times = thumbs_up_times + 1 where id = #{id}")
    void updateThumbsUpTimes(@Param("id") Long id);
}
